package cn.chenli.job.dao.impl;

import cc.tuhaolicai.mt.predefined.database.PageInfo;
import cn.chenli.job.dao.BaseDao;
import cn.chenli.job.tool.ToolMongo;
import org.asyou.mongo.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * BaseDao的实现
 *
 * @author chenli
 * @create 2017-06-13
 **/
public class BaseDaoImpl<T> implements BaseDao<T> {
    @Override
    public T save(T t) {
        return ToolMongo.insertOne(t);
    }

    @Override
    public T findOne(T t) {
        return ToolMongo.findOne(t);
    }

    @Override
    public Page<T> findPage(T t, PageInfo pageInfo) {
        return ToolMongo.findPage(t, pageInfo);
    }

    @Override
    public List<T> findAll(T t) {
        return ToolMongo.find(t, null, false, false).getList();
    }

    @Override
    public Boolean update(T oldModel, T newModel) {
        return ToolMongo.updateOne(oldModel, newModel);
    }

    @Override
    public Boolean delete(T t) {
        return ToolMongo.deleteOne(t);
    }

    @Override
    public List<T> convert(List<T> list) {
        if (list == null || list.size() < 1) {
            return new ArrayList<>();
        }
        List<T> oldList = findAll(list.get(0));
        //删除所有
        oldList.forEach(ToolMongo::deleteOne);
        return ToolMongo.insertMany(list);
    }

}